#include "../../source/http/http.hpp"

// 辅助函数：打印测试结果
void printTestResult(bool passed, const std::string &testName) {
    if (passed) {
        std::cout << "[PASSED] " << testName << std::endl;
    } else {
        std::cout << "[FAILED] " << testName << std::endl;
    }
}

void TestRequest(){
    std::cout << "=== Starting HttpRequest Class Tests ===\n";

    // 测试 1: 默认初始化
    {
        HttpRequest req;
        printTestResult(req._method.empty(), "Default _method is empty");
        printTestResult(req._path.empty(), "Default _path is empty");
        printTestResult(req._version == "HTTP/1.1", "Default _version is HTTP/1.1");
        printTestResult(req._body.empty(), "Default _body is empty");
        printTestResult(req._headers.empty(), "Default _headers is empty");
        printTestResult(req._params.empty(), "Default _params is empty");
        printTestResult(req._matches.empty(), "Default _matches is empty");
    }

    // 测试 2: 设置和获取头部字段（大小写不敏感）
    {
        HttpRequest req;
        const std::string key = "Content-Type";
        const std::string value = "application/json";
        req.SetHeader(key, value);

        printTestResult(req.HasHeader("content-type"), "HasHeader (大小写不敏感)");
        printTestResult(req.GetHeader("content-type") == value, "GetHeader (大小写不敏感)");
    }

    // 测试 3: 设置和获取查询参数
    {
        HttpRequest req;
        const std::string key = "page";
        const std::string value = "1";
        req.SetParam(key, value);

        printTestResult(req.GetParam(key) == value, "GetParam returns correct value");
        printTestResult(req.GetParam("nonexistent") == "", "GetParam returns empty for non-existent key");
    }

    // 测试 4: ReSet 方法
    {
        HttpRequest req;
        req._method = "POST";
        req._path = "/api/data";
        req._body = "test body";
        req.SetHeader("Host", "example.com");
        req.SetParam("id", "123");

        std::cout << req.ToString() << std::endl;
        req.ReSet();

        printTestResult(req._method.empty(), "ReSet clears _method");
        printTestResult(req._path.empty(), "ReSet clears _path");
        printTestResult(req._body.empty(), "ReSet clears _body");
        printTestResult(req._headers.empty(), "ReSet clears _headers");
        printTestResult(req._params.empty(), "ReSet clears _params");
        printTestResult(req._matches.empty(), "ReSet clears _matches");
        printTestResult(req._version == "HTTP/1.1", "ReSet keeps _version");
    }

    // 测试 5: ContentLength 方法
    {
        HttpRequest req;

        // 有效 Content-Length
        req.SetHeader("Content-Length", "1234");
        printTestResult(req.ContentLength() == 1234, "ContentLength returns correct value");

        // 无效 Content-Length
        req.SetHeader("Content-Length", "invalid");
        printTestResult(req.ContentLength() == 0, "ContentLength handles invalid value");

        // 无 Content-Length
        req.ReSet();
        printTestResult(req.ContentLength() == 0, "ContentLength returns 0 when header missing");
    }

    // 测试 6: Close 方法
    {
        HttpRequest req;

        // 默认无 Connection 头
        printTestResult(req.Close() == true, "Close returns true when header missing");

        // Connection: close
        req.SetHeader("Connection", "close");
        printTestResult(req.Close() == true, "Close returns true for 'close'");

        // Connection: keep-alive
        req.SetHeader("Connection", "keep-alive");
        printTestResult(req.Close() == false, "Close returns false for 'keep-alive'");
    }

    std::cout << "=== All Tests Completed ===\n";
}

void TestResponse(){
    HttpResponse res;

    // 测试大小写不敏感的头部字段
    res.SetHeader("Content-Type", "application/json");
    printTestResult(res.GetHeader("content-type") == "application/json", "大小写不敏感的头部字段 - 存在");
    printTestResult(res.GetHeader("CONTENT-TYPE") == "application/json", "大小写不敏感的头部字段 - 全大写");

    // 测试 SetHeader 覆盖
    res.SetHeader("Content-Type", "text/plain");
    printTestResult(res.GetHeader("Content-Type") == "text/plain", "SetHeader 覆盖已有键");

    // 测试 Close 方法
    res.SetHeader("Connection", "keep-alive");
    printTestResult(res.Close() == false, "Close - keep-alive 为长连接");
    res.SetHeader("Connection", "close");
    printTestResult(res.Close() == true, "Close - close 为短连接");
    res.ReSet();
    printTestResult(res.Close() == true, "Close - 无 Connection 字段为短连接");

    // 测试 SetRedirect
    res.SetRedirect("http://example.com", 301);
    printTestResult(res._redirect_flag == true, "SetRedirect - 标志位设置");
    printTestResult(res._redirect_url == "http://example.com", "SetRedirect - URL 设置");
    printTestResult(res._status == 301, "SetRedirect - 状态码设置");

    std::cout << "=== 所有测试完成 ===" << std::endl;
}

int main() {
    TestResponse();
    // TestRequest();
    
    return 0;
}